package com.starbaba.module.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.module.model.LoanContentItemWithVal;
import com.starbaba.module.service.ActivityVarsService;

@Component
public class ActivityVarsServiceImpl implements ActivityVarsService {

    @SuppressWarnings("deprecation")
    static BaseDao dao = new BaseDao(Constant.POOL_LIFE);

    //根据mid查询该模板下的字段key
    @Override
    public List<LoanContentItemWithVal> getKeyList(Long mid) throws AppException {
        // TODO Auto-generated method stub
        String sql = "select name, content, remark, sort, type from module_content_item where modelId = ? order by sort asc";
        List<Object> params = new ArrayList<Object>();
        params.add(mid);
        List<LoanContentItemWithVal> list = dao.list(sql, LoanContentItemWithVal.class, params);
        return list;
    }

    //根据key list查询字段值
    @Override
    public List<LoanContentItemWithVal> getValList(List<LoanContentItemWithVal> activityVarsValList, Integer aid) throws AppException {

        //定义一个用于赋值后返回的副本
        List<LoanContentItemWithVal> _activityVarsValList = activityVarsValList;

        // TODO Auto-generated method stub
        Integer size = activityVarsValList.size();
        if(size > 0){
            for (int i = 0; i < size; i++){

                //搜索匹配aid和字段key
                String sql = "select val from loan_content_activity_vars where aid = " + aid + " and content = '" + activityVarsValList.get(i).getContent() + "'";  
                List<LoanContentItemWithVal> list = dao.list(sql, LoanContentItemWithVal.class);

                try{
                    if(list.size() > 0){
                        _activityVarsValList.get(i).setVal(list.get(0).getVal());
                    }
                }catch(Exception e){
                    LogService.error(e);
                }
            }
        }
        return _activityVarsValList;
    }
}
